<template>
  <div class="layui-fluid layui-form">
    <div class="layui-card">
      <input type="hidden" v-model="system.id" />
      <div class="layui-row">
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">系统标识</label>
            <div class="layui-input-block">
              <input
                type="text"
                v-model="system.systemKey"
                v-if="isDisableId"
                readonly="true"
                placeholder="请输入系统标识"
                autocomplete="off"
                class="layui-input layui-disabled"
                maxlength="20"
              />
              <input
                type="text"
                v-model="system.systemKey"
                v-else
                placeholder="请输入系统标识"
                autocomplete="off"
                class="layui-input"
                maxlength="20"
              />
            </div>
          </div>
        </div>
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">系统名称</label>
            <div class="layui-input-block">
              <input
                type="text"
                v-model="system.systemName"
                placeholder="请输入系统名称"
                autocomplete="off"
                class="layui-input"
                maxlength="25"
              />
            </div>
          </div>
        </div>
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">超时时间</label>
            <div class="layui-input-block">
              <input
                type="text"
                v-model="system.taskTimeout"
                lay-verify="number"
                placeholder="请输入用户任务超时时间"
                autocomplete="off"
                class="layui-input"
                maxlength="5"
              />
            </div>
          </div>
        </div>
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">超时类型</label>
            <div class="layui-input-block">
              <select
                lay-filter="change"
                lay-event="getTaskTimeoutType"
                :lay-select="system.taskTimeoutType.id"
              >
                <option value="">--请选择--</option>
                <option
                  v-for="(ch, chIndex) in dataType"
                  :value="ch.id"
                  v-html="ch.desc"
                ></option>
              </select>
            </div>
          </div>
        </div>
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">临期时间</label>
            <div class="layui-input-block">
              <input
                type="text"
                v-model="system.taskApproachTimeout"
                lay-verify="number"
                placeholder="请输入用户任务超时时间类型"
                autocomplete="off"
                class="layui-input"
                maxlength="5"
              />
            </div>
          </div>
        </div>
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">临期类型</label>
            <div class="layui-input-block">
              <select
                lay-filter="change"
                lay-event="getTaskApproachTimeoutType"
                :lay-select="system.taskApproachTimeoutType.id"
              >
                <option value="">--请选择--</option>
                <option
                  v-for="(ch, chIndex) in dataType"
                  :value="ch.id"
                  v-html="ch.desc"
                ></option>
              </select>
            </div>
          </div>
        </div>
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">超时开启类型</label>
            <div class="layui-input-block">
              <select
                lay-filter="change"
                lay-event="getTaskStartTimeoutType"
                :lay-select="system.startTimeoutType"
              >
                <option value="create">任务创建时</option>
                <option value="claim">任务签收时</option>
              </select>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-row">
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">设置超时接口</label>
            <div class="layui-input-block">
              <input
                type="checkbox"
                lay-skin="switch"
                :lay-checkbox="isTimeout"
                lay-filter="change"
                lay-event="setTimeoutConfig"
                lay-text="是|否"
              />
            </div>
          </div>
        </div>
      </div>
      <InterfaceConfig
        ref="timeoutConfig"
        :config="system.timeoutConfig"
        v-if="isTimeout"
      ></InterfaceConfig>
      <div class="layui-row">
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">设置用户接口</label>
            <div class="layui-input-block">
              <input
                type="checkbox"
                lay-skin="switch"
                :lay-checkbox="isUserTask"
                lay-filter="change"
                lay-event="setUserTaskInterface"
                lay-text="是|否"
              />
            </div>
          </div>
        </div>
      </div>
      <InterfaceConfig
        ref="userTaskConfig"
        :config="system.userTaskConfig"
        v-if="isUserTask"
      ></InterfaceConfig>
      <JsonView
        :dataResult="userTask"
        :errorData="userTaskError"
        v-if="isUserTask"
      ></JsonView>
      <div class="layui-row">
        <div class="layui-col-md4">
          <div class="layui-form-item">
            <label class="layui-form-label">设置服务接口</label>
            <div class="layui-input-block">
              <input
                type="checkbox"
                lay-skin="switch"
                lay-filter="change"
                :lay-checkbox="isServiceTask"
                lay-event="setServiceTaskInterface"
                lay-text="是|否"
              />
            </div>
          </div>
        </div>
      </div>
      <InterfaceConfig
        ref="serviceTaskConfig"
        :config="system.serviceTaskConfig"
        v-if="isServiceTask"
      ></InterfaceConfig>
      <JsonView
        :dataResult="serviceTask"
        :errorData="serviceTaskError"
        v-if="isServiceTask"
      ></JsonView>
      <div class="layui-form-item" style="text-align: center">
        <label class="layui-btn" lay-submit v-on:click="saveSystemInfo"
          >提交保存</label
        >
      </div>
    </div>
  </div>
</template>
<script>
import InterfaceConfig from "../../../common/InterfaceConfig";
import JsonView from "../../../common/JsonView";
export default {
  name: "ActSystemEdit",
  components: { InterfaceConfig, JsonView },
  data() {
    return {
      system: {
        id: "",
        taskTimeoutType: {
          id: null,
        },
        taskApproachTimeoutType: {
          id: null,
        },
      },
      userTask: {
        code: "为000000时接口调用成功，调用成功必须包含结果集result",
        msg: "操作成功",
        reuslt: {
          variable:
            "接口调用是会返回variable参数集合信息，包含用户任务当前任务所需的变量信息",
          variable1: "",
          variable2: "",
        },
      },
      userTaskError: {
        code: "不为000000时接口调用失败，调用失败可以包含结果集result",
        msg: "xxxxxx错误",
        result: null,
      },
      serviceTask: {
        code: "为000000时接口调用成功",
        msg: "操作成功",
        reuslt: {},
      },
      serviceTaskError: {
        code: "不为000000时接口调用失败，调用失败可以包含结果集result",
        msg: "xxxxxx错误",
        result: null,
      },
      dataType: {},
      userTaskInterface: {},
      serviceTaskInterface: {},
      isUserTask: false,
      isServiceTask: false,
      serviceTaskConfig: {},
      timeoutConfig: {},
      userTaskConfig: {},
      isDisableId: false,
      isTimeout: false,
    };
  },
  mounted: function () {
    this.system.id = this.$route.query.id;
    this.initLayui();
    this.loadDateType();
    this.loadSystemData();
    this.userTaskConfig = this.$refs.userTaskConfig;
    this.serviceTaskConfig = this.$refs.serviceTaskConfig;
    this.timeoutConfig = this.$refs.timeoutConfig;
    this.serviceTaskConfig = this.$refs.serviceTaskConfig;
    if (!this.checkIsNull(this.system.id)) {
      this.isDisableId = true;
    }
  },
  methods: {
    loadSystemData: function () {
      var that = this;
      if (!this.checkIsNull(this.system.id)) {
        var obj = {
          systemId: this.system.id,
        };
        this.post(this.actSystem + "/systemInfo", obj, function (data) {
          if (data.code == "000000") {
            that.system = data.result;
            if (that.checkIsNull(that.system.taskTimeoutType)) {
              that.system.taskTimeoutType = {
                id: null,
              };
            }
            if (that.checkIsNull(that.system.taskApproachTimeoutType)) {
              that.system.taskApproachTimeoutType = {
                id: null,
              };
            }
            if (data.result.userTaskConfig != null) {
              that.isUserTask = true;
            }
            if (data.result.serviceTaskConfig != null) {
              that.isServiceTask = true;
            }
            if (data.result.timeoutConfig != null) {
              that.isTimeout = true;
            }
          } else {
            that.showMsg(data.msg, false);
          }
        });
      }
    },
    loadDateType: function () {
      var that = this;
      this.post(this.common + "/dataType", function (data) {
        if (data.code == "000000") {
          that.dataType = data.result;
        } else {
          that.openMsg(data.msg, false);
        }
      });
    },
    getTaskTimeoutType: function (data, e) {
      this.system.taskTimeoutType = {
        id: data,
      };
    },
    getTaskApproachTimeoutType: function (data, e) {
      this.system.taskApproachTimeoutType = {
        id: data,
      };
    },
    getTaskStartTimeoutType: function (data) {
      this.system.startTimeoutType = data;
    },
    setUserTaskInterface: function (data, e) {
      if (data.checked) {
        // 显示用户接口设置页面
        this.isUserTask = true;
      } else {
        // 隐藏用户接口设置页面
        this.isUserTask = false;
      }
    },
    setServiceTaskInterface: function (data, e) {
      if (data.checked) {
        // 显示服务接口设置页面
        this.isServiceTask = true;
      } else {
        // 隐藏服务接口设置页面
        this.isServiceTask = false;
      }
    },
    setTimeoutConfig: function (data, e) {
      if (data.checked) {
        // 显示超时接口设置页面
        this.isTimeout = true;
      } else {
        // 隐藏超时接口设置页面
        this.isTimeout = false;
      }
    },
    saveSystemInfo: function () {
      var that = this;
      var isok = this.checkSaveData();
      if (!isok) {
        return;
      }
      this.post(this.actSystem + "/saveData", this.system, function (data) {
        if (data.code == "000000") {
          that.openMsg(data.msg, true);
          that.closeParentOpen();
        } else {
          that.showMsg(data.msg, false);
        }
      });
    },

    checkSaveData: function () {
      if (this.checkIsNull(this.system.systemKey)) {
        this.showMsg("请输入系统标识");
        return false;
      }
      if (this.checkIsNull(this.system.systemName)) {
        this.showMsg("请输入系统名称");
        return false;
      }
      if (this.userTaskConfig) {
        var isOk = this.userTaskConfig.checkNodeInfo("用户任务接口，");
        if (!isOk) {
          return false;
        } else {
          this.system.userTaskConfig = this.userTaskConfig.getData();
        }
      }
      if (this.isServiceTask) {
        var isOk = this.serviceTaskConfig.checkNodeInfo("服务任务接口，");
        if (!isOk) {
          return false;
        } else {
          this.system.serviceTaskConfig = this.serviceTaskConfig.getData();
        }
      }
      if (this.isTimeout) {
        var isOk = this.timeoutConfig.checkNodeInfo("任务超时接口，");
        console.log(isOk);
        if (!isOk) {
          return false;
        } else {
          this.system.timeoutConfig = this.timeoutConfig.getData();
        }
      }
      if (
        !this.checkIsNull(this.system.taskTimeout) &&
        this.checkIsNull(this.system.taskTimeoutType.id)
      ) {
        this.showMsg("请选择用户任务超时时间类型");
        return false;
      }
      if (
        !this.checkIsNull(this.system.taskApproachTimeout) &&
        this.checkIsNull(this.system.taskApproachTimeoutType.id)
      ) {
        this.showMsg("请选择用户任务临期时间类型");
        return false;
      }
      if (
        !this.checkIsNull(this.system.taskApproachTimeoutType.id) &&
        this.checkIsNull(this.system.taskApproachTimeout)
      ) {
        this.showMsg("请输入用户任务临期时间");
        return false;
      }
      if (
        !this.checkIsNull(this.system.taskTimeoutType.id) &&
        this.checkIsNull(this.system.taskTimeout)
      ) {
        this.showMsg("请输入用户任务超时时间");
        return false;
      }
      if (
        (!this.checkIsNull(this.system.taskTimeout) ||
          !this.checkIsNull(this.system.taskApproachTimeout)) &&
        this.isTimeout &&
        this.checkIsNull(this.system.timeoutConfig)
      ) {
        this.showMsg("请设置任务超时接口");
        return false;
      }
      if (!this.isServiceTask) {
        this.system.serviceTaskConfig = null;
      }
      if (!this.isUserTask) {
        this.system.userTaskConfig = null;
      }
      if (!this.isTimeout) {
        this.system.timeoutConfig = null;
      }
      return true;
    },
  },
  updated: function () {
    this.initLayui();
    this.userTaskConfig = this.$refs.userTaskConfig;
    this.serviceTaskConfig = this.$refs.serviceTaskConfig;
    this.timeoutConfig = this.$refs.timeoutConfig;
  },
};
</script>